package cn.chinasyq.photoquan.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.View;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.view.ItemButtonDialog;

public class CameraHead {
    /**
     * 调用相册的请求代码
     */
    public static final int ALBLUM = 0x2804;

    /**
     * 调用拍照的请求代码
     */
    public static final int PHOTOGRAPH = 0x2805;

    /**
     * 剪裁的的请求代码
     */
    public static final int PHOTORESOULT = 0x2806;

    /**
     * 照片文件类型
     */
    private static final String IMAGE_UNSPECIFIED = "image/*";

    /**
     * 拍照时，临时存储一个副本到根目录下的temp.jpg
     */
    private static final String PHOTOGRAPH_TEMP = Environment
            .getExternalStorageDirectory() + "/temp.jpg";
    private static final File PHOTOGRAPH_TEMP_C = new File(Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "sheying" + System.currentTimeMillis() + ".jpg");
    public static final Uri imageUri = Uri.fromFile(new File(PHOTOGRAPH_TEMP));
    //照相保存
    public static final Uri imageUri1 = Uri.fromFile(PHOTOGRAPH_TEMP_C);
    private static String attr;

    /**
     * 选择图像获取方式
     */
    public static void selectModeDialog(final Activity activity) {
        String[] items = new String[]{activity.getString(R.string.camear),
                activity.getString(R.string.gralley),};
        ItemButtonDialog dialog = new ItemButtonDialog(activity,
                items);
        dialog.setOnItemClickListener(new ItemButtonDialog.OnItemClickListener() {
            @Override
            public void onItemClick(View v, String txt, int index) {
                switch (index) {
                    case 0:
                        Photograph(activity);
                        break;
                    case 1:
                        Album(activity);
                        break;
                    default:
                        break;
                }
            }
        });
        dialog.show();
    }



    public static void selectModeDialog(final Fragment fragment) {
        String[] items = new String[]{fragment.getString(R.string.camear),
                fragment.getString(R.string.gralley),};
        ItemButtonDialog dialog = new ItemButtonDialog(fragment.getActivity(),
                items);
        dialog.setOnItemClickListener(new ItemButtonDialog.OnItemClickListener() {
            @Override
            public void onItemClick(View v, String txt, int index) {
                switch (index) {
                    case 0:
                        Photograph(fragment);
                        break;
                    case 1:
                        Album(fragment);
                        break;
                    default:
                        break;
                }
            }
        });
        dialog.show();
    }

    /**
     * 调用相册 调用该方法需要在指定的activity页面中添加onActivityResult回调方法
     *
     * @param activity Activity 传递指定的上下文环境
     */
    public static void Album(Activity activity) {
        activity.startActivityForResult(getAlbumIntent(activity), ALBLUM);
    }

    public static void Album(Fragment fragment) {
        fragment.startActivityForResult(getAlbumIntent(fragment.getActivity()), ALBLUM);
    }

    private static Intent getAlbumIntent(Context c) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        intent.putExtra("scaleUpIfNeeded", true);// 去黑边
        intent.putExtra("scale", true);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        return intent;
    }

    /**
     * 调用拍照 调用该方法需要在指定的activity页面中添加onActivityResult回调方法
     *
     * @param activity Activity 传递指定的上下文环境
     */
    public static void Photograph(Activity activity) {
        activity.startActivityForResult(getPhotographIntent(), PHOTOGRAPH);
    }

    public static void Photograph(Fragment fragment) {
        fragment.startActivityForResult(getPhotographIntent(), PHOTOGRAPH);
    }

    private static Intent getPhotographIntent() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 保存一个临时的照片副本
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri1);
        return intent;
    }


    private static Intent getPhotoZoomIntent(Uri uri,
                                             int outputX, int outputY) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (outputX != 0 && outputY != 0) {
            intent.putExtra("aspectX", outputX);
            intent.putExtra("aspectY", outputY);
            intent.putExtra("outputX", outputX);
            intent.putExtra("outputY", outputY);
        }
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", true); // no face detection
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("scaleUpIfNeeded", true);// 去黑边
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        return intent;
    }

    public static void cropImageUri(Fragment fragment, Uri uri, int requestCode, int outputX, int outputY) {
        fragment.startActivityForResult(getPhotoZoomIntent(uri, outputX, outputY), requestCode);
    }

    public static void cropImageUri(Activity fragment, Uri uri, int requestCode, int outputX, int outputY) {
        fragment.startActivityForResult(getPhotoZoomIntent(uri, outputX, outputY), requestCode);
    }

    /**
     * 拍照和相册回调处理方法（调用缩放取景功能）
     *
     * @param activity    Activity 传递应用上下文环境
     * @param requestCode int 请求代码
     * @param resultCode  int 返回的代码
     * @param data
     * @return Uri
     */
    public static Uri callbackZoom(Activity activity, int requestCode,
                                   int resultCode, Intent data, int outputX, int outputY) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
        Uri bitmap = null;
        if (Activity.RESULT_OK == resultCode) {
            if (requestCode == CameraHead.ALBLUM) {
                attr = BitmapUtil.readEXIF(BitmapUtil.convertUri2FilePath(activity, data.getData()));
                CameraHead.cropImageUri(activity, data.getData(), CameraHead.PHOTORESOULT, outputX, outputY);
            } else if (requestCode == CameraHead.PHOTOGRAPH) {
                attr = BitmapUtil.readEXIF(BitmapUtil.convertUri2FilePath(activity, CameraHead.imageUri1));
                CameraHead.cropImageUri(activity, CameraHead.imageUri1, CameraHead.PHOTORESOULT, outputX, outputY);
            } else if (requestCode == CameraHead.PHOTORESOULT) {
                if (imageUri != null) {
                    if (attr != null) {
                        BitmapUtil.writeEXIF(BitmapUtil.convertUri2FilePath(activity, imageUri), attr);
                    }
                    bitmap = CameraHead.imageUri;
                }
            }
        }
        return bitmap;
    }

    public static Uri callbackZoom(Fragment fragment, int requestCode,
                                   int resultCode, Intent data, int outputX, int outputY) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
        Uri bitmap = null;
        if (Activity.RESULT_OK == resultCode) {
            if (requestCode == CameraHead.ALBLUM) {
                attr = BitmapUtil.readEXIF(BitmapUtil.convertUri2FilePath(fragment.getActivity(), data.getData()));
                CameraHead.cropImageUri(fragment, data.getData(), CameraHead.PHOTORESOULT, outputX, outputY);
            } else if (requestCode == CameraHead.PHOTOGRAPH) {
                attr = BitmapUtil.readEXIF(BitmapUtil.convertUri2FilePath(fragment.getActivity(), CameraHead.imageUri1));
                CameraHead.cropImageUri(fragment, CameraHead.imageUri1, CameraHead.PHOTORESOULT, outputX, outputY);
            } else if (requestCode == CameraHead.PHOTORESOULT) {
                if (imageUri != null) {
                    if (attr != null) {
                        BitmapUtil.writeEXIF(BitmapUtil.convertUri2FilePath(fragment.getActivity(), imageUri), attr);
                    }
                    bitmap = CameraHead.imageUri;
                }
            }
        }
        return bitmap;
    }



}
